<?php
class Database
{
	private $conn;
	
	private $HOST = 'localhost';
	private $BASE = 'eg152391_finance';
	private $USER = 'eg152391_finance';
	private $PASS = 'vT#8ylT1DKC&';
	
	public function __construct()
	{
		$this->conn = @new mysqli($this->HOST, $this->USER, $this->PASS, $this->BASE);
		if (mysqli_connect_errno() !== 0)
		{
			$msg = mysqli_connect_error();
			echo $msg;
			exit;
		}
		@$this->conn->query('SET NAMES \'utf8\'');
	}

	static public function instance()
	{
		static $objDB;
		
		if(!isset($objDB))
		{
			$objDB = new Database;
		}
		
		return $objDB;
	}

	function real_escape_string($val)
	{
		return @$this->conn->real_escape_string($val);
	}
	
	function selectAll($sql)
	{
		$output = array();
		
		$results = @$this->conn->query($sql);
		if($results === FALSE or $results === NULL)
		{
			echo 'Błąd wykonania zapytania - ('.$sql.')';
		}else{//2012-08-18 dodany warunek
			while(($row = @$results->fetch_object()) !== NULL)
			{
				$output[] = $row;
			}
			$results->close();
		}
		
		return $output;
	}

	function selectCount($sql)
	{
		$results = $this->conn->query($sql);
		if($results === FALSE or $results === NULL)
		{
			echo 'Błąd wykonania zapytania - ('.$sql.') - '.$this->conn->error;
		}
		$row = $results->num_rows;

		$results->close();
		return $row;
	}
	
	function queryCustom($sql)
	{
		$results = $this->conn->query($sql);
		if($results === FALSE or $results === NULL)
		{
			echo 'Błąd wykonania zapytania - ('.$sql.') - '.$this->conn->error;
		}

        return $this->conn->affected_rows;
	}
	
	function selectOne($sql)
	{
		$results = $this->conn->query($sql);
		if($results === FALSE or $results === NULL)
		{
			echo 'Błąd wykonania zapytania - ('.$sql.') - '.$this->conn->error;
		}
		$row = @$results->fetch_object();

		$results->close();
		return $row;
	}
	
	function update($tableName, $arUpdates, $sWhere = null)
	{
		$arSet = array();
		
		foreach($arUpdates as $name => $value)
		{
			//$arSet[] = $name . ' = ' . Database::quote_smart($value); //real_escape_string
            //$arValueList[] = "'" . $this->real_escape_string($value) . "'";
            $arSet[] = $name . " = '" .$this->real_escape_string($value) ."'";
		}
		
		$sSet = implode(', ', $arSet);
		
		$sql = "UPDATE $tableName SET $sSet";
		if($sWhere)
		{
			$sql .= " WHERE $sWhere";
		}
		
		$result = $this->conn->query($sql);
		if(!$result)
		{
			echo 'Błąd wykonania zapytania - ('.$sql.') - '.$this->conn->error;
			//echo '<script type="text/javascript">alert("Błąd wykonania zapytania: ($sql) $this->conn->error")</script>';
		}
		
		return $this->conn->affected_rows;
	}

	function insert($tableName, $arValues, $id = null)
	{
		$sFieldList = join(', ', array_keys($arValues));
		$arValueList = array();
		
		foreach($arValues as $value)
		{
			//$arValueList[] = $this->quote_smart($value); //real_escape_string
			//$arValueList[] = "'" . $value . "'";
            $arValueList[] = "'" . $this->real_escape_string($value) . "'";
		}
		
		$sValueList = implode(', ', $arValueList);
		
		$sql = "INSERT INTO $tableName ( $sFieldList ) VALUES ( $sValueList )";
		$result = $this->conn->query($sql);
		
		return $id ? $this->conn->insert_id : $this->conn->affected_rows;
	}
	
	function replace($tableName, $arValues, $id = null)
	{
		$sFieldList = join(', ', array_keys($arValues));
		$arValueList = array();
		
		foreach($arValues as $value)
		{
			$arValueList[] = "'" . $this->real_escape_string($value) . "'";
		}
		
		$sValueList = implode(', ', $arValueList);
		
		$sql = "REPLACE INTO $tableName ( $sFieldList ) VALUES ( $sValueList )";
		
        $result = $this->conn->query($sql);
		
		return $id ? $this->conn->insert_id : $this->conn->affected_rows;
	}
	
	function insertDuplicate($tableName, $arUpdates)
	{
		$arSet = array();
		$sFieldList = join(', ', array_keys($arUpdates));
		
		foreach($arUpdates as $value)
		{
			$arValueList[] = "'" . $this->real_escape_string($value) . "'";
		}
		
		$sValueList = implode(', ', $arValueList);
		
		foreach($arUpdates as $name => $value)
		{
			$arSet[] = $name . " = '" .$this->real_escape_string($value) ."'";
		}
		
		$sSet = implode(', ', $arSet);
		
		$sql = "INSERT INTO $tableName ( $sFieldList ) VALUES ($sValueList) ON DUPLICATE KEY UPDATE $sSet";
		
		$result = $this->conn->query($sql);
		if(!$result)
		{
			echo 'Błąd wykonania zapytania - ('.$sql.') - '.$this->conn->error;
		}
		
		return $this->conn->affected_rows;
	}
	
	function delete($tableName, $sWhere = null)
	{
		$sql = "DELETE FROM $tableName";
		
		if($sWhere)
		{
			$sql .= " WHERE $sWhere";
		}
		
		$result = $this->conn->query($sql);
		
		return $this->conn->affected_rows;
	}
	
	function truncate($table)
	{
		$this->conn->query("TRUNCATE TABLE $table");
	}
	
	function lockTables($tableName)
	{
		$sql = "LOCK TABLES $tableName WRITE";
		
		$result = $this->conn->query($sql);
	}
	
	function unlockTables()
	{
		$sql = "UNLOCK TABLES";
		
		$result = $this->conn->query($sql);
	}
}

// stare rozwiazanie //
class Database22
{
	private static $conn;

	public function __construct()
	{
		$this->conn = @new mysqli('localhost', 'electron', 'zufzuWNjFn3Z6DHW', 'electron');
		if (mysqli_connect_errno() !== 0)
		{
			$msg = mysqli_connect_error();
			echo $msg;
			exit;
		}
		@$this->conn->query('SET NAMES \'utf8\'');
	}

	static public function instance()
	{
		static $objDB;
		
		if(!isset($objDB))
		{
			$objDB = new Database();
		}
		
		return $objDB;
	}

	function quote_smart($value)
	{
		// Stripslashes
		if (get_magic_quotes_gpc())
		{
            $value = stripslashes($value);
		}
      // Quote if not a number or a numeric string
		if (!is_numeric($value))
		{
			//$value = "'" . mysql_real_escape_string($value) . "'";
			$value = "'" . $value . "'";
		}
		
		return $value;
	}
	
	function real_escape_string($val)
	{
		return @$this->conn->real_escape_string($val);
	}
	
	function selectAll($sql)
	{
		$output = array();
		
		$results = @$this->conn->query($sql);
		if($results === FALSE or $results === NULL)
		{
			echo 'Błąd wykonania zapytania - ('.$sql.')';
		}
		while(($row = @$results->fetch_object()) !== NULL)
		{
			$output[] = $row;
		}
		$results->close();
		return $output;
	}

	function selectCount($sql)
	{
		$results = $this->conn->query($sql);
		if($results === FALSE or $results === NULL)
		{
			echo 'Błąd wykonania zapytania - ('.$sql.') - '.$this->conn->error;
		}
		$row = $results->num_rows;

		$results->close();
		return $row;
	}
	
	function queryCustom($sql)
	{
		$results = $this->conn->query($sql);
		if($results === FALSE or $results === NULL)
		{
			echo 'Błąd wykonania zapytania - ('.$sql.') - '.$this->conn->error;
		}
		
		return $this->conn->affected_rows;
	}
	
	function selectOne($sql)
	{
		$results = $this->conn->query($sql);
		if($results === FALSE or $results === NULL)
		{
			echo 'Błąd wykonania zapytania - ('.$sql.') - '.$this->conn->error;
		}
		$row = @$results->fetch_object();

		$results->close();
		return $row;
	}
	
	function update($tableName, $arUpdates, $sWhere = null)
	{
		$arSet = array();
		
		foreach($arUpdates as $name => $value)
		{
			$arSet[] = $name . " = '" .$this->real_escape_string($value) ."'";
		}
		
		$sSet = implode(', ', $arSet);
		
		$sql = "UPDATE $tableName SET $sSet";
		if($sWhere)
		{
			$sql .= " WHERE $sWhere";
		}
		
		$result = $this->conn->query($sql);
		if(!$result)
		{
			echo 'Błąd wykonania zapytania - ('.$sql.') - '.$this->conn->error;
			//echo '<script type="text/javascript">alert("Błąd wykonania zapytania: ($sql) $this->conn->error")</script>';
		}
		
		return $this->conn->affected_rows;
	}

	function insert($tableName, $arValues, $id = null)
	{
		$sFieldList = join(', ', array_keys($arValues));
		$arValueList = array();
		
		foreach($arValues as $value)
		{
			//$arValueList[] = $this->quote_smart($value);
			//$arValueList[] = "'" . $value . "'";
            $arValueList[] = "'" . $this->real_escape_string($value) . "'";
		}
		
		$sValueList = implode(', ', $arValueList);
		
		$sql = "INSERT INTO $tableName ( $sFieldList ) VALUES ( $sValueList )";
		$result = $this->conn->query($sql);
		
		return $id ? $this->conn->insert_id : $this->conn->affected_rows;
	}
	
	function replace($tableName, $arValues, $id = null)
	{
		$sFieldList = join(', ', array_keys($arValues));
		$arValueList = array();
		
		foreach($arValues as $value)
		{
			$arValueList[] = $this->real_escape_string($value);
		}
		
		$sValueList = implode(', ', $arValueList);
		
		$sql = "REPLACE INTO $tableName ( $sFieldList ) VALUES ( $sValueList )";
		$result = $this->conn->query($sql);
		
		return $id ? $this->conn->insert_id : $this->conn->affected_rows;
	}
	
	function replace2($tableName, $arUpdates)
	{
		$arSet = array();
		$sFieldList = join(', ', array_keys($arUpdates));
		
		foreach($arUpdates as $value)
		{
			$arValueList[] = $this->quote_smart($value);
		}
		
		$sValueList = implode(', ', $arValueList);
		
		foreach($arUpdates as $name => $value)
		{
			$arSet[] = $name . ' = ' . Database::quote_smart($value);
		}
		
		$sSet = implode(', ', $arSet);
		
		//$sql = "REPLACE INTO $tableName SET $sSet";
		
		$sql = "INSERT INTO $tableName ( $sFieldList ) VALUES ($sValueList) ON DUPLICATE KEY UPDATE $sSet";
		
		$result = $this->conn->query($sql);
		if(!$result)
		{
			echo 'Błąd wykonania zapytania - ('.$sql.') - '.$this->conn->error;
			//echo '<script type="text/javascript">alert("Błąd wykonania zapytania: ($sql) $this->conn->error")</script>';
		}
		
		return $this->conn->affected_rows;
	}
	
	function delete($tableName, $sWhere = null)
	{
		$sql = "DELETE FROM $tableName";
		
		if($sWhere)
		{
			$sql .= " WHERE $sWhere";
		}
		
		$result = $this->conn->query($sql);
		
		return $this->conn->affected_rows;
	}
	
	function truncate($tableName)
	{
		$this->conn->query("TRUNCATE TABLE $tableName");
	}
	
	function lockTables($tableName)
	{
		$sql = "LOCK TABLES $tableName WRITE";
		
		$result = $this->conn->query($sql);
	}
	
	function unlockTables()
	{
		$sql = "UNLOCK TABLES";
		
		$result = $this->conn->query($sql);
	}
}
?>